package com.studio49.exmple.service.web.sys.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.studio49.exmple.common.constant.SysConstant;
import com.studio49.exmple.common.exception.BusinessException;
import com.studio49.exmple.common.util.MD5Utils;
import com.studio49.exmple.entity.web.sys.Menus;
import com.studio49.exmple.entity.web.sys.User;
import com.studio49.exmple.entity.web.sys.bo.SessionUserBo;
import com.studio49.exmple.mapper.web.sys.UserMapper;
import com.studio49.exmple.service.web.sys.MenusService;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.studio49.exmple.service.web.sys.UserService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private MenusService menusService;

	@Override
	public Short login(String loginName, String password, HttpServletRequest request)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, BusinessException {
		Short role = null;
		/** 验证登录名,密码 */
		if (StringUtils.isBlank(loginName)) {
			throw new BusinessException("登录名不能为空!");
		}
		if (StringUtils.isBlank(password)) {
			throw new BusinessException("密码不能为空!");
		}
		User user = this.getByLoginNameAndPassword(loginName, password);
		if (user != null) { // 找到用户
			role = user.getRole();
			if (user.getIsEnable().intValue() == 0) {
				throw new BusinessException("该用户已被冻结,请联系管理员!");
			}
		} else {
			throw new BusinessException("请检查用户名和密码");
		}

		/** 以下为用户名密码验证通过,把用户菜单封装对象BO放到session */
		HttpSession session = request.getSession();
		SessionUserBo sessionUserBo = new SessionUserBo();// session用户菜单封装对象BO
		PropertyUtils.copyProperties(sessionUserBo, user);
		
		//获取菜单
		List<Menus> menuList = menusService.findByUserId(user.getId());
		sessionUserBo.setMenuList(menuList);

		session.setAttribute(SysConstant.CURRENT_USER_OBJECT, sessionUserBo);

		/** 登录成功记录日志 */
//		UserLog userLog = new UserLog();
//		userLog.setUserId(user.getId());
//		userLog.setLoginName(user.getLoginName());
//		userLog.setType((short) 1);
//		userLog.setOperateName("登录");
//		userLog.setUserName(user.getUserName());
//		userLog.setClientIP(PubUtils.getRemoteHost(request));
//		userLog.setHostIP(PubUtils.getLocalIP());
//		Address address = AddressUtils.getAddresses();
//		if (address != null) {
//			userLog.setCity(address.getCity());
//		}
//		userLog.setCreateTime(new Date());
//		try {
//			userLogService.save(userLog);
//		} catch (FieldUniquenessException e) {
//			e.printStackTrace();
//			LOG.error("记录登录日志失败");
//		}
		return role;
	}

//	@Override
	public User getByLoginNameAndPassword(String loginName, String password) {
		
		String md5 = MD5Utils.md5(password);
		
		Example example = new Example(User.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("loginName", loginName);
		criteria.andEqualTo("password", md5);
		criteria.andEqualTo("userType", 2);
		List<User> list = userMapper.selectByExample(example);
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
	
}
